// 小程序访问记录
import { DataTypes, UUIDV4 } from 'sequelize';
import { MySqlModel, getSequelize } from '../db';
import { getToday } from '../utils';

class MiniAccessLog extends MySqlModel {

}

MiniAccessLog.init({
  id: {
    type: DataTypes.UUID, unique: 'id', allowNull: false, defaultValue: UUIDV4, validate: { isUUID: 4 }, primaryKey: true, comment: '主键'
  },
  hotel_id: {
    type: DataTypes.UUID, allowNull: false, validate: { isUUID: 4 }, comment: '主体'
  },
  openid: { type: DataTypes.STRING(50), allowNull: true, comment: '用户唯一标识' },
  ip_address: {
    type: DataTypes.STRING(50), allowNull: true, comment: '客户端IP地址'
  },
  business_date: {
    type: DataTypes.DATEONLY, allowNull: false, comment: '日期'
  },
  created_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '更新时间'
  }
}, {
  sequelize: getSequelize(),
  tableName: 'mini_access_log',
  comment: '小程序访问记录'
});

MiniAccessLog.beforeValidate((instance, options) => {
  if (instance.isNewRecord) {
    instance.business_date = getToday();
  }
})

export default MiniAccessLog;
